/**
 * Created by lx on 2017/5/8.
 */
angular.module('module.invite', [
    'ngCookies'
])

    .config(['$stateProvider', '$urlRouterProvider', function ($stateProvider, $urlRouterProvider) {
        $stateProvider
            .state('register', {
                url: '/register',
                templateUrl: 'view/invite/register.html',
                controller: 'RegisterController'
            })
            .state('invite', {
                url: '/invite?linkid&access&userid',
                templateUrl: 'view/invite/invite.html',
                controller: 'InviteController'
            })
    }])
    .controller('RegisterController', ['$scope', '$state','$cookieStore','AuthService','$interval','$timeout',
        function ($scope,$state,$cookieStore,AuthService,$timeout,$interval) {
           /* $scope.register={};
            $scope.share={};
            $scope.myuser={};

            //模态框显示
            var registerbtn= document.getElementById("goregister");
            //var bbbe=document.getElementById("#my_alert").style.display="none"
            $scope.usershow=false;
            $scope.right_phone=false;
            $scope.errorphone=false;
            $scope.my_alert=false;
            $scope.congratulation=false;

            $scope.goregister=function(){
                //判断手机号是否为空
                if($scope.register.phone==null||$scope.register.phone==""){
                    $scope.usershow=true;
                    $scope.errorphone=true;
                    $scope.right_phone=false;
                    console.log($scope.usershow)
                } else{
                    //判断手机号码是否有效
                    if(isTelCode($scope.register.phone)){
                        if(myinviti("userid")){
                            //判断电话号码是否注册过
                            AuthService.getuser($scope.register.phone).$promise.then(function(success){
                                if(success.userid==myinviti("userid")){
                                    $scope.myuser.userId=success.userid;
                                    $scope.myuser.access = myinviti("access");
                                    $scope.myuser.templateId=$cookieStore.get("selecttplid");
                                    AuthService.producecode($scope.myuser,function(success){
                                        $cookieStore.put("my_linkid",success.linkId);
                                        disab(registerbtn);
                                        $timeout(function(){
                                            $state.go('invite')
                                        }, 1000);
                                    },function(error){
                                        console.log(error);
                                    })
                                }else{
                                    for(var i=0;i<=4;i++){
                                        $interval(function(){
                                            $("#my_alert").addClass("myhide").fadeIn(200).fadeOut(200);
                                        },2000,1);
                                    }
                                    console.log("您输入的电话号码非本账号");
                                }

                                //$state.go('invite')
                            },function(error){
                                console.log("您好，您输入的电话号码不符合")
                            })
                    }else{
                             $scope.register.sign="11";
                             $scope.register.name=$scope.register.phone;
                             $scope.register.logo=null;
                             $scope.register.password="12345"
                             AuthService.saveshareuserid($scope.register,function(success){
                             disab(registerbtn);
                             //用户入库得到邀请码
                             $scope.share.userId=success.userid;
                             $scope.share.access = myinviti("access");
                             $scope.share.templateId=$cookieStore.get("selecttplid");
                             AuthService.producecode($scope.share,function(success){
                                 console.log(success)
                                 //绑定上级接口
                                 $scope.subuser={};
                                 $scope.subuser.userId=success.userId;
                                 $scope.subuser.invite= myinviti("inviteid");
                                 console.log($scope.subuser)
                                 AuthService.getsubinvite($scope.subuser,function(success){
                                     console.log("success",success)
                                 },function(error){
                                     console.log("error",error)
                                 })
                                 $cookieStore.put("my_linkid",success.linkId);
                                 $scope.usershow=true;
                                 $scope.right_phone=true;
                                 $scope.errorphone=false;
                                 $timeout(function(){
                                 $state.go('invite')
                                 }, 2000);
                                 },function(error){
                                    console.log(error);
                                 })
                                     },function(error){
                                    $scope.register="";
                                 })

                        }
                    }else{
                        for(var i=0;i<=4;i++){
                            $interval(function(){
                                $("#my_alert").addClass("myhide").fadeIn(200).fadeOut(200);
                            },2000,1);
                        }
                    }

                }
            }

            //模态框点击消失
            $scope.fadeout=function(){
                $("#modal_in_").fadeOut(1000,function(){
                    $scope.usershow=false;
                    $scope.errorphone=false;
                    console.log( $scope.usershow,"hello")
                });
                $("#modal_in").fadeOut(1000,function(){
                    $scope.usershow=false;
                    $scope.errorphone=false;
                });
            }*/
    }])
    .controller('InviteController', ['$scope', '$state','$cookieStore','$stateParams','AuthService',function ($scope,$state,$cookieStore,$stateParams,AuthService) {
            //$scope.myinvite=$cookieStore.get("my_invite");
            //var sharelink=$cookieStore.get("my_linkid");
            var sharelink=$stateParams.linkid;
            AuthService.getmycode(sharelink).$promise.then(function(invite){
                $scope.myinvite=invite.invite;
                //获取下级列表
                var myujunior={
                    userId:$stateParams.userid,
                    access:$stateParams.access,
                };
                AuthService.get_junior(myujunior,function(inviteList){
                    $scope.myjuniors=[];
                    for(var i=0;i<inviteList.length;i++){
                        AuthService.getuserdetail(inviteList[i].userId,function(success){
                            $scope.myjuniors.push(success)
                        },function(error){
                            console.log(error)
                        })
                    }
                },function(error){
                    $scope.myjuniors=[];
                })
            },function(error){
                //$state.go("register");
            });

        //拼接地址
        $scope.my_share=function(){
            var goinvite=document.getElementById("goshare_");
            goinvite.setAttribute("disabled", "disabled");
            $(goinvite).addClass("invite_btn_disable");
            var url="https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx277ac1ea258fab7a&redirect_uri=http://pay.shuwang.info/wmapp/xwm?access="+myinviti("access")+"&invite="+$scope.myinvite+"&shopid="+myinviti("shopid")+"&userid="+myinviti("userid")+"&response_type=code&scope=snsapi_base&state=1#wechat_redirect"
            alert(url)
        }
    }])